package android;

import java.beans.PropertyChangeEvent;

import network.NetworkHandler;
import stockrobot.android.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class stockrobotActivity extends Activity {
    /** Called when the activity is first created. */
	ProgressDialog pd;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Button button = (Button) findViewById(R.id.connectBtn);
        button.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		Handler refresh = new Handler(Looper.getMainLooper()); 
        		refresh.post(new Runnable() { 
        		    public void run() 
        		    { 
        		    	showConnecting(); 
        		    	new Thread() {
        		    		public void run() {
        		    			if(!useFakeServer()){
        		    				tryConnect();
        		    			} else {
        		    				fakeConnect();
        		    			}
        		    		}
        		    	}.start();
        		    }
        		}); 
        		}

       
        	});
        }
    
    private boolean useFakeServer(){
    	final CheckBox fakebox = (CheckBox) findViewById(R.id.chckbxFakeServer);
    	return fakebox.isChecked();
    }
    
	private void fakeConnect() {
	    Intent prefIntent = new Intent(this,TabsViewPagerFragmentActivity.class);   
	    pd.dismiss();
	    this.startActivity(prefIntent); 
	    NetworkHandler.instance().fakeServer();
	}
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
	    	AlertDialog alertDialog = new AlertDialog.Builder(this).create();
	    	alertDialog.setMessage("Exit ASTRo Stock-Robot?");
	        alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {   
	            public void onClick(DialogInterface dialog, int which) {   
	            	 finish();
	            	 System.exit(0);  
	          } });   
	        alertDialog.setButton2("No", new DialogInterface.OnClickListener() {   
	            public void onClick(DialogInterface dialog, int which) {   
	            	return;
	          }}); 
	        alertDialog.show();
        }
        return true;
    }



    private void showConnecting(){
		 pd = ProgressDialog.show(stockrobotActivity.this, "Connecting to ASTRo", "Please wait...");
    }
    
    private void changeScreen(){
	    Intent prefIntent = new Intent(this,TabsViewPagerFragmentActivity.class);   
	    this.startActivity(prefIntent); 
    }
    
    private void hideConnecting(){
    	pd.dismiss();
    }
    
	private void tryConnect(){
    	String password = ((TextView) findViewById(R.id.tvPassword)).getText().toString();
    	String hostname = ((TextView) findViewById(R.id.adressTextField)).getText().toString();
    	int portNumber  = Integer.parseInt( ((TextView) findViewById(R.id.portNumber)).getText().toString());
    	 if(NetworkHandler.instance().connect(hostname, portNumber, password)){
     		Handler refresh = new Handler(Looper.getMainLooper()); 
     		refresh.post(new Runnable() { 
     		    public void run() 
     		    { 
     		    	hideConnecting();
		    		changeScreen();
     		    }
     		}); 
		    } else {
        		Handler refresh = new Handler(Looper.getMainLooper()); 
        		refresh.post(new Runnable() { 
        		    public void run() 
        		    { 
        		    	hideConnecting();
        		    	showErrorDialog();
        		    	new Thread() {
        		    		public void run() {
        		    		}
        		    	}.start();
        		    }
        		}); 
		    }
	}

	private void showErrorDialog() {
    	AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    	alertDialog.setTitle("Error! Can't connect to ASTRo.");
    	alertDialog.setMessage("Please check that ASTRo is running and the right port is open.");
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {   
            public void onClick(DialogInterface dialog, int which) {   
              return;   
          } });   
        alertDialog.show();
	}

}
